clear all;
clc;

patnum = 2;
pattag=num2str(patnum);
dir1=['patient',num2str(patnum),'_08192014'];
dir2=['p',num2str(patnum),'_act_curves'];
addpath(dir1);
addpath([dir1,'\',dir2]);

% ------------------------- File Information ------------------------------

epidur = 1;
if(epidur==1)
    loctag = 'epi';
    if(patnum==1)
        ftag='0p3';
    end
    if(patnum==2)
        ftag='0p45';
    end
    ttl = 'Epidural Stimulation';
else
    loctag = 'sub';
    if(patnum==1)
        ftag='0p2';
    end
    if(patnum==2)
        ftag='0p12';
    end
    ttl = 'Intradural Stimulation';
end

% Bryan's designs
ac1 = -load(['neg_',loctag,'_bp_dc_act_p',pattag,'_d',ftag,'_t0.txt']);
ac1 = sort(ac1(:,1));
ar1 = -load(['neg_',loctag,'_bp_dr_act_p',pattag,'_d',ftag,'_t0.txt']);
ar1 = sort(ar1(:,1));

ac2 = load(['pos_',loctag,'_atp_dc_act_p',pattag,'_d',ftag,'_t0.txt']);
ac2 = sort(ac2(:,1));
ar2 = load(['pos_',loctag,'_atp_dr_act_p',pattag,'_d',ftag,'_t0.txt']);
ar2 = sort(ar2(:,1));

ac3 = load(['pos_',loctag,'_tt1_dc_act_p',pattag,'_d',ftag,'_t0.txt']);
ac3 = sort(ac3(:,1));
ar3 = load(['pos_',loctag,'_tt1_dr_act_p',pattag,'_d',ftag,'_t0.txt']);
ar3 = sort(ar3(:,1));

ra_bpsub=2/0.013261;
ra_atpsub=1/0.005924;
% ac1=ac1/ra_bpsub/1e3;
% ar1=ar1/ra_bpsub/1e3;
% ac2=ac2/ra_atpsub/1e3;
% ar2=ar2/ra_atpsub/1e3;

% ------------------------- Selectivity -----------------------------------

noaxons=length(ac1);
peract=(1/noaxons):(100-1/noaxons)/(noaxons-1):100;

fvsf_1 = pera_vs_perb(ac1,ar1);
fvsf_2 = pera_vs_perb(ac2,ar2);
fvsf_3 = pera_vs_perb(ac3,ar3);

[m,n] = size(fvsf_1);

farea1 = trapz(fvsf_1(:,1),fvsf_1(:,2))/100;
farea2 = trapz(fvsf_2(:,1),fvsf_2(:,2))/100;
farea3 = trapz(fvsf_3(:,1),fvsf_3(:,2))/100;

tdr_1 = 100*sum(fvsf_1(:,2)<=0)/m;
tdr_2 = 100*sum(fvsf_2(:,2)<=0)/m;
tdr_3 = 100*sum(fvsf_3(:,2)<=0)/m;

% ------------------------- Plotting --------------------------------------


msub = [1,5:5:m,m];

figure;
hold on;
plot(0:100,0:100,'k--','LineWidth',5);
plot(fvsf_1(msub,2),fvsf_1(msub,1),'k.-','LineWidth',2,'MarkerSize',30);
plot(fvsf_2(msub,2),fvsf_2(msub,1),'k^-','LineWidth',2,'MarkerSize',10);
plot(fvsf_3(msub,2),fvsf_3(msub,1),'k-','LineWidth',4,'MarkerSize',12,...
                                     'MarkerFaceColor','k');

title(ttl,'FontSize',30);                                 
ylabel('Dorsal Columns Activated (%)','FontSize',30);
xlabel('Dorsal Roots Activated (%)','FontSize',30);
L1 = 'AD-TECH';
L2 = 'AT';
L3 = 'TT-1';
legend('No Selectivity',L1,L2,L3,'Location','SE');
set(gca,'FontSize',25);
set(gca,'XTick',0:25:100,'YTick',0:25:100);
axis square;

return

% INSET
axes('Position',[0.6,0.48,0.2,0.2]);
box on;
hold on;
plot(fvsf_1(msub,2),fvsf_1(msub,1),'k.-','LineWidth',2,'MarkerSize',30);
plot(fvsf_2(msub,2),fvsf_2(msub,1),'k^-','LineWidth',2,'MarkerSize',10);
plot(fvsf_3(msub,2),fvsf_3(msub,1),'k-','LineWidth',4,'MarkerSize',12,...
                                     'MarkerFaceColor','k');

hold off;
axis square;
ylim([40,90]);
xlim([0,8]);
set(gca,'XTick',[0,8],'YTick',[60,90],'FontSize',20);

figure;
subplot(1,2,1);
hold on;
plot(ac1,peract,'k','LineWidth',4);
plot(ar1,peract,'k--','LineWidth',4);
hold off;
title('AD-TECH','FontSize',30);
ylabel('Percent Activated','FontSize',30);
xlabel('Applied Current (mA)','FontSize',30);
set(gca,'FontSize',30);
xlim([0,6.3]);

subplot(1,2,2);
hold on;
plot(ac2,peract,'k','LineWidth',4);
plot(ar2,peract,'k--','LineWidth',4);
hold off;
title('AT','FontSize',30);
xlabel('Applied Current (mA)','FontSize',30);
set(gca,'FontSize',30,'YTick',[]);
xlim([0,6.3]);
legend('DC fibers','DR fibers','Location','SE');

% subplot(2,3,6);
% hold on;
% plot(0:100,0:100,'k--','LineWidth',5);
% plot(fvsf_1(msub,1),fvsf_1(msub,2),'k^-','LineWidth',3,'MarkerSize',12);
% plot(fvsf_2(msub,1),fvsf_2(msub,2),'ko--','LineWidth',3,'MarkerSize',12);
% plot(fvsf_3(msub,1),fvsf_3(msub,2),'k-','LineWidth',4,'MarkerSize',12,...
%                                      'MarkerFaceColor','k');
% % plot(fvsf_4(msub,1),fvsf_4(msub,2),'ko-','LineWidth',2,'MarkerSize',12,...
% %                                      'MarkerFaceColor','k');
% L1 = [' (DR_0 =',num2str(tdr_1,2),')'];
% L2 = [' (DR_0 =',num2str(tdr_2,2),')'];
% L3 = [' (DR_0 =',num2str(tdr_3,2),')'];
% legend('No Selectivity',L1,L2,L3,'Location','NW');
% set(gca,'FontSize',25);
% set(gca,'YTick',[]);
% axis([10,40,0,25]);
